<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Agentorder extends Backend
{
    
    /**
     * YOnlineorder模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('YOnlineorder');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        $startime = (int)$this->request->param('ids');
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            //搜索条件
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            $map = [];
            foreach ($filter_arr as $key=>$val){
                if($key == 'addtime'){
                    $time_arr = explode(',', $filter_arr['addtime']);
                    $star = $this->getMilliDate($time_arr[0].' 0:0:0');
                    $end = $this->getMilliDate($time_arr[1].' 23:59:59');
                    if(empty($time_arr[0])){
                        $map['addtime'] = array('<=', $end);
                    }elseif(empty($time_arr[1])){
                        $map['addtime'] = array('>=', $star);
                    }else{
                        $map['addtime'] = array('between', [$star,$end]);
                    }
                }else{
                    $map[$key] = $val;
                }
            }
            $map_count = $map;
            if(isset($map_count['status'])){
                unset($map_count['status']);
            }
            $totalall = Db::name('y_onlineorder')->where($map_count)->where(['channel'=>1,'status'=>2])->sum('orderamount');//总
            $totalmonth = Db::name('y_onlineorder')->where(['channel'=>1,'status'=>2])->where("FROM_UNIXTIME(addtime/1000,'%Y-%m') = date_format(now(),'%Y-%m')")->sum('orderamount');//月
            $totalweek = Db::name('y_onlineorder')->where(['channel'=>1,'status'=>2])->where("FROM_UNIXTIME(addtime/1000,'%Y-%m-%d %H:%I:%S') > date_sub(now(),interval 7 day) ")->sum('orderamount');//周
            $totaltoday = Db::name('y_onlineorder')->where(['channel'=>1,'status'=>2])->where("FROM_UNIXTIME(addtime/1000,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->sum('orderamount');//今天
            $statistics['totalall'] = $totalall?:0;
            $statistics['totalmonth'] =$totalmonth?:0;
            $statistics['totalweek'] =$totalweek?:0;
            $statistics['totaltoday'] =$totaltoday?:0;
            
            $total = $this->model
            ->where($map)
            ->where(['channel'=>1])
            ->order($sort, $order)
            ->count();
    
            $list = $this->model
            ->where($map)
            ->where(['channel'=>1])
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
            
            if(empty($list)){
                $result = array("total" => $total, "rows" => $list, "statistics"=>$statistics, "map"=>empty($map)?0:1);
                return json($result);
            }
            
            //处理数据
            $oid_arr = $uid_arr = $map_log = $map_user = $log_arr = $goods_arr = $user_arr = [];
            foreach ($list as $val){
                $oid_arr[] = $val['ID'];
                $uid_arr[] = $val['uid'];
            }
            $map_log['oid'] = array('in',$oid_arr);
            $map_user['uid'] = array('in',$uid_arr);
            $user_list = $this->selectTbale($this->GameDBConfig,'user',$map_user,'uid,nickname');//用户列表
            $log_list = Db::table('fa_y_orderlog')->where($map_log)->select();//订单记录
            $goods_list = Db::table('fa_y_ordergoods')->where($map_log)->select();//订单商品
            foreach ($user_list as $val){
                $user_arr[$val['uid']] = $val['nickname'];
            }
            foreach ($goods_list as $val){
                $goods_arr[$val['oid']] = $val;
            }
            foreach($log_list as $val){
                $log_arr[$val['oid']] = $val;
            }
            
            foreach ($list as $key=>$val){
                $list[$key]['nickname'] = isset($user_arr[$val['uid']])?$user_arr[$val['uid']]:'-';
                $list[$key]['bfnum'] = isset($log_arr[$val['ID']])?$log_arr[$val['ID']]['bfnum']:0;
                $list[$key]['afnum'] = isset($log_arr[$val['ID']])?$log_arr[$val['ID']]['afnum']:0;
                $list[$key]['quotanum'] = isset($goods_arr[$val['ID']])?($goods_arr[$val['ID']]['quotanum']*$goods_arr[$val['ID']]['goodsnum']):0;
                $list[$key]['givenum'] = isset($goods_arr[$val['ID']])?($goods_arr[$val['ID']]['givenum']*$goods_arr[$val['ID']]['goodsnum']):0;
            }

            $result = array("total" => $total, "rows" => $list, "statistics"=>$statistics, "map"=>empty($map)?0:1);
    
            return json($result);
        }
        $this->assign('startime',empty($startime)?'':date('Y-m-d',$startime));
        return $this->view->fetch();
    }
    

}
